<template>
  <div class="department" id="department">
    <!-- 右侧 -->
    <div class="department_right">
      <!-- 顶部导航栏 -->
      <div class="mechanism_nav">
        <!-- 左侧 -->
        <div class="mechanism_nav_left">
          <el-radio-group
            v-model="radio"
            size="small"
            @change="modelType($event)"
          >
            <el-radio-button label="待办"></el-radio-button>
            <el-radio-button label="已办"></el-radio-button>
            <!-- #2b5a9d -->
          </el-radio-group>
        </div>
        <!-- 顶部按钮 -->
        <div class="print">
          <div class="printTable">
            <el-button
              type="primary"
              icon="el-icon-refresh"
              size="small"
              @click="yj_searchForm(getList)"
            ></el-button>
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="small"
              v-if="getList.modelType == '0' && yj_add == true"
              @click="edit('新增')"
              >新增</el-button
            >
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="small"
              v-if="getList.modelType == '0' && yj_edit == true"
              @click="edit('编辑')"
              >编辑</el-button
            >
            <el-button
              type="primary"
              icon="el-icon-circle-check"
              size="small"
              v-if="getList.modelType == '0' && yj_submit == true"
              @click="SubmitText(fId)"
              >提交</el-button
            >

            <el-button
              type="primary"
              icon="el-icon-delete"
              size="small"
              v-if="getList.modelType == '0' && yj_delete == true"
              @click="rowDelete(fId)"
              >删除</el-button
            >
          </div>
        </div>
      </div>

      <!-- 表格 -->
      <div class="text_table">
        <tableData
          :height="navigatepageNums > 1 ? 'calc(100% - 50px)' : '100%'"
          :config="table_config"
          :dataList="tableData"
          :loading="loading"
          @selection-change="selectionChange"
          @sort-change="sortChange"
          @click-change="rowChange"
          @ClickOnLine="yj_viewForm"
          :page="getList.page"
          :pageSize="getList.pageSize"
        />
        <pagination
          v-if="navigatepageNums > 1"
          :info="queryCriteriaData"
          :treelength="treelength"
          :total="queryCriteriaData.total"
          :pageNum.sync="getList.page"
          :limit.sync="getList.pageSize"
          @pagination="yj_searchForm(getList)"
        />
      </div>
      <!--班组首件信息弹框-->
      <div class="Control-1 mechanism_dialogS closure">
        <el-dialog
          :title="dialogTitle"
          :modal-append-to-body="false"
          v-dialogDrag
          :visible.sync="dialogEditVisible"
          v-loading="loadingEditDialog"
          element-loading-text="拼命加载中"
          element-loading-spinner="el-icon-loading"
          element-loading-background="rgba(0, 0, 0, 0.2)"
        >
          <div
            class="dialogBody common"
            :style="{ height: '650px' }"
            style="overflow: hidden"
          >
            <div style="overflow-y: auto; margin-bottom: 20px; height: 100%">
              <div class="baseTitle">第三方检测信息</div>
              <!-- -->
              <div
                class="publicForm"
                style="height: calc(100% - 80px); overflow-y: auto"
              >
                <publicForm
                  ref="publicForm"
                  lableWidth="110px"
                  :formObj="FromShow"
                  :formData="formData"
                  :timestamp="formData.fCheckfile"
                  :fileList="fSourcefileList"
                  @selectChange="selectChange"
                  @inputChange="inputChange"
                  @delteFile="delteFile"
                  @inputFocus="inputFocus"
                >
                </publicForm>
              </div>

              <div style="height: 50px"></div>
            </div>
          </div>
          <div class="newlyAdded_btn" v-if="btnShow">
            <el-button type="primary" @click="yj_saveForm(0)">保存</el-button>

            <el-button type="warning" @click="dialogEditVisible = false"
              >关闭</el-button
            >
          </div>
        </el-dialog>
      </div>
      <!-- 网格弹框列表 -->
      <div class="Control-1 mechanism_dialogS closure">
        <el-dialog
          title="首件工程隶属质量网格列表"
          :modal-append-to-body="false"
          v-dialogDrag
          :visible.sync="dialogEditVisibleGrid"
          v-loading="loadingEditDialogGrid"
          element-loading-text="拼命加载中"
          element-loading-spinner="el-icon-loading"
          element-loading-background="rgba(0, 0, 0, 0.2)"
        >
          <div
            class="dialogBody common"
            :style="{ height: '650px' }"
            style="overflow: hidden"
          >
            <div
              style="
                overflow-y: auto;
                margin-bottom: 20px;
                height: calc(100% - 40px);
              "
            >
              <tableData
                :height="
                  navigatepageNumsGrid > 1 ? 'calc(100% - 50px)' : '100%'
                "
                :config="table_configGrid"
                :dataList="tableDataGrid"
                @selection-change="selectionChange"
                @sort-change="sortChange"
                @click-change="rowChangeGrid"
                :page="getList.page"
                :pageSize="getList.pageSize"
                :selecData="rowGrid"
                ref="multipleTable"
              />
              <pagination
                v-if="navigatepageNumsGrid > 1"
                :info="queryCriteriaDataGrid"
                :treelength="treelength"
                :total="queryCriteriaDataGrid.total"
                :pageNum.sync="getListGrid.page"
                :limit.sync="getListGrid.pageSize"
                @pagination="yj_searchFormGrid(getListGrid)"
              />
            </div>
            <div class="newlyAdded_btn">
              <el-button type="primary" @click="yj_saveFormGrid()"
                >保存</el-button
              >
              <el-button type="warning" @click="dialogEditVisibleGrid = false"
                >关闭</el-button
              >
            </div>
          </div>
        </el-dialog>
      </div>
    </div>
  </div>
</template>
<script>
import {
  getCheckinformationPageLists, //列表
  CheckinformationByFid, //详情
  saveCheckinformation, //保存
  deleteCheckinformation, //删除
  commitCheckinformation, //提交
  getQualityDutyStationLists, //网格列表
} from "@/api/request.js";
import TableData from "@/components/stationProductionPage/HntManage/el-tables"; //主页面列表
import Pagination from "@/components/public/el-pagination"; //主页面分页
import publicForm from "@/components/public/el-form.vue"; //弹框form
import formJs from "./regexDetail.js"; //新增弹框配置项

export default {
  inject: ["reload"],
  name: "ThreeCheckText",
  data() {
    return {
      name: "ThreeCheckText",
      // 分页
      navigatepageNums: null, //是否显示分页
      queryCriteriaData: {},
      // 分页
      yj_add: null,
      yj_edit: null,
      yj_delete: null,
      yj_submit: null,
      radio: "待办",

      // 列表
      getList: {
        fCompanyid: "", // 左侧选中组织机构id
        page: 1, //页码
        pageSize: 20, //每页条数
        userId: "", // 查询待办或已办时传 列表页查询不传
        modelType: "", // 0代办 1已办 2表示列表页查询
      },
      fId: null, //当前行数据fId
      row: { fstatus: "" }, //选中行
      loading: false, //列表转载
      table_config: formJs.table_configText,
      tableData: [], //表格列表数据列表
      // 列表

      // 弹框
      timeYMD: null, //当前日期
      btnShow: true,
      dialogTitle: "",
      loadingEditDialog: false, //弹框loading
      dialogEditVisible: false, //新增弹框是否显示
      FromShow: null,
      FromShowText: formJs.FromShowText, //信息编辑配置参数
      FromShowLook: formJs.FromShowLook, //信息编辑配置参数
      formData: formJs.formData,
      recordfiles: [], //删除文件
      fSourcefileList: [],
      saveData: {},
      // 质量网格弹框
      dialogEditVisibleGrid: false,
      loadingEditDialogGrid: false,
      table_configGrid: formJs.table_configGrid,
      tableDataGrid: [], //表格列表数据列表
      navigatepageNumsGrid: null, //是否显示分页
      queryCriteriaDataGrid: {},
      rowGrid: [],
      getListGrid: {
        fCompanyid: "", // 左侧选中组织机构id
        page: 1, //页码
        pageSize: 10, //每页条数
        userId: "", // 查询待办或已办时传 列表页查询不传
        modelType: "2", // 0代办 1已办 2表示列表页查询
        fQualitygridtype: "", // 质量网格类别
        fProjectposition: "", // 工程部位
      },
      Qualitygridclass: [], //工程质量网格多选
      Qualitygridclassid: [], //工程质量网格多选
      // 质量网格弹框
      // 弹框
      userInfo: {}, //登录用户
    };
  },
  components: {
    TableData, //主页面列表
    Pagination, //分页
    publicForm, //表单
  },
  created() {},
  mounted() {
    this.userInfo = JSON.parse(sessionStorage.getItem("yjSmart-userInfo"));
    // 质量网格弹框
    this.getListGrid = {
      fCompanyid: this.userInfo.companyId, // 左侧选中组织机构id
      page: 1, //页码
      pageSize: 10, //每页条数
      userId: "", // 查询待办或已办时传 列表页查询不传
      modelType: "2", // 0代办 1已办 2表示列表页查询
      fQualitygridtype: "", // 质量网格类别
      fProjectposition: "", // 工程部位
    };
    // 质量网格弹框
    this.getList = {
      fCompanyid: this.userInfo.companyId, // 左侧选中组织机构id
      userId: this.userInfo.userId, // 查询待办或已办时传 列表页查询不传
      page: 1, //页码
      pageSize: 20, //每页条数
      modelType: "0", // 0代办 1已办 不传或传空 表示列表页查询
    };
    this.getHeight();
    this.getData();
    this.yj_searchForm(this.getList);
    this.btnRole(this.name);

    this.table_config.operation.cols = [
      {
        prop: "fStatus",
        type: "primary",
        handleRow: this.handleRow,
      },
    ];
    this.table_config.operation["specialClass"] = function (rowData) {
      var color = null;
      if (rowData == "已完成") {
        color = "green !important";
      } else if (rowData == "编辑中") {
        color = "red !important";
      }
      return color;
    };
  },
  methods: {
    btnRole(name) {
      var btnList = [];
      var ModuleList = JSON.parse(sessionStorage.getItem("yjSmart-ModuleList"));
      if (ModuleList.length > 0) {
        ModuleList.map((item) => {
          if (item.fEncode == name) {
            btnList = item.modulebuttonList;
          }
        });
        var btnStr = [];
        btnList.map((item) => {
          btnStr.push(item.fEncode);
        });
        this.yj_add = btnStr.includes("yj_add");
        this.yj_edit = btnStr.includes("yj_edit");
        this.yj_delete = btnStr.includes("yj_delete");
        this.yj_submit = btnStr.includes("yj_submit");
      } else {
        this.yj_add = true;
        this.yj_edit = true;
        this.yj_delete = true;
        this.yj_submit = true;
      }
    },

    modelType(e) {
      this.fId = "";
      this.row = "";
      if (e == "待办") {
        this.getList.modelType = "0";
        this.getList.userId = this.userInfo.userId;
      } else {
        this.getList.modelType = "1";
        this.getList.userId = this.userInfo.userId;
      }
      this.yj_searchForm(this.getList);
    },

    // 获取列表数据
    yj_searchForm(id) {
      this.row = { fstatus: "" }; //选中行
      this.loading = true;
      getCheckinformationPageLists(id)
        .then((res) => {
          if (res.errcode == 0) {
            var that = this;
            that.loading = false;
            that.tableData = res.data.pageInfo.list;
            that.queryCriteriaData = res.data.pageInfo;
            that.navigatepageNums =
              that.queryCriteriaData.navigatepageNums.length;
          } else {
            this.$message.error("列表", res.msg);
            this.loading = false;
          }
        })
        .catch(() => {
          this.loading = false;
        });
    },

    edit(title) {
      this.formData = {
        fId: "",
        fProjectname: "", // 所属项目名称
        fCompanyname: "", // 所属标段名称
        fCheckunit: "", // 第三方检查单位
        fCheckunitid: "", // 第三方检查单位id
        fCheckdates: "", // 检查时间
        fCheckopinion: "", // 检查情况概述
        fChangeresult: "", // 是否要求整改
        fCheckfile: "", // 本次检查相关资料
        fQualitygridclass: "", // 涉及工程质量网格
        fQualitygridclassid: "", // 涉及工程质量网格id
        fWorkstatus: "", // 工作是否结束
        fMaintainername: "", // 维护人
        fMaintainerid: "", // 维护id
        fMaintaindates: "", // 维护时间
        fStatus: "",
        fNote: "",
        fIsdeleted: "",
      };
      this.dialogTitle = title;
      this.recordfiles = [];
      this.fSourcefileList = [];
      this.btnShow = true;
      this.FromShow = this.FromShowText;

      if (title == "新增") {
        var fCheckfile = "fCheckfile" + new Date().valueOf();
        this.formData = {
          fId: "",
          fProjectname: this.userInfo.projectName, // 所属项目名称
          fCompanyname: this.userInfo.companyName, // 所属标段名称
          fCheckunit: "", // 第三方检查单位
          fCheckunitid: "", // 第三方检查单位id
          fCheckdates: this.timeYMD, // 检查时间
          fCheckopinion: "", // 检查情况概述
          fChangeresult: "", // 是否要求整改
          fCheckfile: fCheckfile, // 本次检查相关资料
          fQualitygridclass: "", // 涉及工程质量网格
          fQualitygridclassid: "", // 涉及工程质量网格id
          fWorkstatus: "", // 工作是否结束
          fMaintainername: this.userInfo.realName, // 维护人
          fMaintainerid: this.userInfo.userId, // 维护id
          fMaintaindates: this.timeYMD, // 维护时间
          fStatus: "",
          fNote: "",
          fIsdeleted: "",
        };
        this.dialogEditVisible = true;
      } else {
        if (this.fId == "" || this.fId == null) {
          this.$message.error("请先选择需要编辑的数据");
        } else {
          this.loadingEditDialog = true; //弹框loading
          this.dialogEditVisible = true;
          this.getConcrete(this.fId);
        }
      }
    },
    // 获取详情
    getConcrete(id) {
      CheckinformationByFid({ fId: id })
        .then((res) => {
          if (res.errcode == 0) {
            this.loadingEditDialog = false; //弹框loading
            this.formData = res.data.yjQmpMsCheckinformationThirdparty;

            if (
              this.formData.fCheckdates == "" ||
              this.formData.fCheckdates == null
            ) {
              this.formData.fCheckdates = this.timeYMD; // 检查时间
            }
            if (
              this.formData.fMaintaindates == "" ||
              this.formData.fMaintaindates == null
            ) {
              this.formData.fMaintaindates = this.timeYMD; // 维护时间
            }
            this.formData.fMaintainername = this.userInfo.realName; //填报人
            this.formData.fMaintainerid = this.userInfo.userId;
            this.fSourcefileList = res.data.fCheckfilelist; //本次检查相关资料
          } else {
            this.loadingEditDialog = false; //弹框loading
            return this.$message.error(res.msg);
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    // 保存
    yj_saveForm() {
      if (this.$refs.publicForm.submitForm()) {
        if (this.fSourcefileList.length != 0) {
          this.saveData = {
            fId: this.formData.fId, //传此表示修改
            userId: this.userInfo.userId, //当前登录人id
            userName: this.userInfo.realName, // 当前登录人名称
            fProjectname: this.userInfo.projectName, // 当前登录人项目名称
            fProjectid: this.userInfo.projectId, // 当前登录人项目ID
            fCompanyname: this.userInfo.companyName, // 当前登录人标段名称
            fCompanyid: this.userInfo.companyId, // 当前登录人标段ID
            fCheckunit: this.formData.fCheckunit, // 第三方检查单位
            fCheckunitid: this.formData.fCheckunitid, // 第三方检查单位id
            fCheckdates: this.formData.fCheckdates, // 检查时间
            fCheckopinion: this.formData.fCheckopinion, // 检查情况概述
            fChangeresult: this.formData.fChangeresult, // 是否要求整改
            fCheckfile: this.formData.fCheckfile, // 本次检查相关资料
            fQualitygridclassid: this.formData.fQualitygridclassid, // 涉及工程质量网格id
            fQualitygridclass: this.formData.fQualitygridclass, // 涉及工程质量网格
            fWorkstatus: this.formData.fWorkstatus, // 工作是否结束
            fMaintainername: this.formData.fMaintainername, // 维护人
            fMaintainerid: this.formData.fMaintainerid, // 维护人id
            fMaintaindates: this.formData.fMaintaindates, // 维护时间
            recordfiles: this.recordfiles, // 删除文件id集合
          };
          saveCheckinformation(this.saveData)
            .then((res) => {
              if (res.errcode == 0) {
                this.yj_searchForm(this.getList);
                this.dialogEditVisible = false;
                this.$message.success("新增成功");
              } else {
                this.$message.error(res.msg);
                console.log("接口报错");
              }
            })
            .catch((error) => {
              console.log(error);
            });
        } else {
          this.$message.error("请检查‘本次检查相关资料’是否上传");
        }
      } else {
        console.log("error");
      }
    },

    // 提交
    SubmitText(id) {
      if (id != "" && id != null) {
        this.$confirm("是否继续提交?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            this.loading = true;
            commitCheckinformation({
              fId: id,
              userId: this.userInfo.userId,
              userName: this.userInfo.realName,
            })
              .then((res) => {
                if (res.errcode == 0) {
                  this.loading = false;
                  this.yj_searchForm(this.getList);
                  this.$message.success("提交成功");
                } else {
                  this.$message.error("提交失败", res.msg);
                  this.loading = false;
                }
              })
              .catch(() => {
                console.log("提交失败");
                this.loading = false;
              });
          })
          .catch(() => {
            this.$message("取消提交");
          });
      } else {
        return this.$message.error("请先选择需要提交的数据");
      }
    },

    // 删除
    rowDelete(id) {
      if (id != "" && id != null) {
        this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            this.loading = true;
            deleteCheckinformation({ fId: id })
              .then((res) => {
                if (res.errcode == 0) {
                  this.loading = false;
                  this.yj_searchForm(this.getList);
                  this.$message.success("删除成功");
                } else {
                  this.$message.error("删除失败", res.msg);
                  this.loading = false;
                }
              })
              .catch(() => {
                console.log("删除失败");
                this.loading = false;
              });
          })
          .catch(() => {
            this.$message("取消删除");
          });
      } else {
        return this.$message.error("请先选择需要删除的数据");
      }
    },

    // 获取页面容器高度
    getHeight() {
      var h = document.documentElement.clientHeight; //获取页面可见高度
      this.dialogHeight = h * 0.36 + "px";
    },
    // 点击查看
    yj_viewForm(row, label) {
      this.row = row;
      this.fId = row.fId;
      if (label == "检查时间") {
        this.editText(this.fId);
      }
    },

    // 查看
    editText(id) {
      this.FromShow = this.FromShowLook;
      this.dialogTitle = "查看";
      this.btnShow = false;
      if (id != "" && id != null) {
        this.loadingEditDialog = true; //弹框loading
        this.dialogEditVisible = true;
        CheckinformationByFid({ fId: id })
          .then((res) => {
            if (res.errcode == 0) {
              var that = this;
              that.loadingEditDialog = false;
              this.formData = res.data.yjQmpMsCheckinformationThirdparty;
              this.fSourcefileList = res.data.fCheckfilelist; //本次检查相关资料
            } else {
              this.$message.error("查看", res.msg);
              this.loadingEditDialog = false;
            }
          })
          .catch(() => {
            console.log("查看---详情接口报错");
            this.loadingEditDialog = false;
          });
      } else {
        return this.$message.error("请先选择需要查看的数据");
      }
    },

    getData() {
      var time = new Date();
      const y = time.getFullYear();
      const m = (time.getMonth() + 1 + "").padStart(2, "0");
      const d = (time.getDate() + "").padStart(2, "0");
      this.timeYMD = `${y}-${m}-${d}`;
    },
    // 点击当前
    rowChange(val) {
      this.row = val;
      this.fId = val.fId;
    },
    // 刪除
    delteFile(file) {
      for (let index = 0; index < this.fSourcefileList.length; index++) {
        if (file.uid == this.fSourcefileList[index].uid) {
          if (this.fSourcefileList[index].atta == undefined) {
            this.recordfiles.push({
              fId: this.fSourcefileList[index].fId,
            });
          } else {
            this.recordfiles.push({
              fId: this.fSourcefileList[index].atta.fId,
            });
          }
          this.fSourcefileList.splice(index, 1);
        }
      }
    },
    // 网格弹框
    rowChangeGrid(val) {
      return val;
    },
    yj_searchFormGrid(id) {
      this.rowGrid = [];
      this.dialogEditVisibleGrid = true;
      this.loadingEditDialogGrid = true;
      getQualityDutyStationLists(id)
        .then((res) => {
          if (res.errcode == 0) {
            this.loadingEditDialogGrid = false;
            var that = this;
            that.loadingGrid = false;
            that.tableDataGrid = res.data.pageInfo.list;
            that.queryCriteriaDataGrid = res.data.pageInfo;
            that.navigatepageNumsGrid =
              that.queryCriteriaDataGrid.navigatepageNums.length;

            that.tableDataGrid.map((item) => {
              that.formData.fQualitygridclassid.split(",").map((v) => {
                if (item.fId == v) {
                  this.rowGrid.push(item);
                }
              });
            });
          } else {
            this.$message.error("网格列表", res.msg);
            this.loadingEditDialogGrid = false;
          }
        })
        .catch(() => {
          this.loadingEditDialogGrid = false;
        });
    },
    yj_saveFormGrid() {
      if (this.rowGrid == null) {
        this.$message.error("请先选择数据,再保存");
      } else {
        this.Qualitygridclass = [];
        this.Qualitygridclassid = [];
        this.rowGrid.map((item) => {
          this.Qualitygridclass.push(item.fProjectposition);
          this.Qualitygridclassid.push(item.fId);
        });
        this.formData.fQualitygridclass = this.Qualitygridclass.join(",");
        this.formData.fQualitygridclassid = this.Qualitygridclassid.join(",");
        this.dialogEditVisibleGrid = false;
      }
    },
    // 网格弹框
    inputChange(value, index, prop) {
      // console.log(value, index, prop);
      return value, index, prop;
    },
    selectChange(value, index, prop) {
      if (value == "内容不实，重新上报。") {
        this.formData.fSignstatusid = 2;
      } else if (value == "情况属实，信息确认。") {
        this.formData.fSignstatusid = 1;
      }
      // console.log(value, index, prop);
      return value, index, prop;
    },
    inputFocus(index, prop) {
      // console.log(index, prop);
      if (index == 4 && prop == "fQualitygridclass") {
        this.yj_searchFormGrid(this.getListGrid);
      }
    },
    // 多选方法
    selectionChange(val) {
      // console.log("多选", val);
      this.rowGrid = val;
    },
    // 排序方法
    sortChange() {},
  },
};
</script>

<style scoped>
.printTables {
  background: #fff;
  display: flex;
  justify-content: end;
  padding: 5px;
  margin-top: 10px;
}

.dialogBody {
  padding: 3px 20px;
  height: 80%;
  overflow-y: auto;
}

.baseTitle {
  /* border: 1px solid #0c204d; */
  border-bottom: 1px solid #0c204d;
  height: 30px;
  line-height: 30px;
  position: relative;
  padding-left: 15px;
}

.baseTitle::after {
  display: block;
  content: "";
  height: 30px;
  width: 5px;
  position: absolute;
  left: 0;
  top: 0;
  background: #0c204d;
}
/* 弹框 */
.publicForm {
  display: flex;
}
.publicForm .mouldForm /deep/ .el-form-item__content {
  margin-left: 120px !important;
}
.publicFormBox {
  width: 50%;
  height: 100%;
}
.closure /deep/ .el-input__suffix {
  left: 91%;
}

/* .department /deep/ .el-table__body tr.current-row > td {
  background-color: aliceblue !important;
} */
/* 弹框 */
</style>